
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jl_plugin/bean/BlueDevice.dart';
import 'package:jl_plugin/bean/EqInfo.dart';
import 'package:jl_plugin/jl_plugin.dart';
import 'package:jl_plugin_example/AppBarLeftTitle.dart';
import 'package:jl_plugin_example/widget/AppColors.dart';
import 'package:flutter_xlider/flutter_xlider.dart';

class BlueEqSettingPage extends StatefulWidget{
  BlueDevice blueDevice;
  BlueEqSettingPage(this.blueDevice);
  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends State<BlueEqSettingPage>{

  @override
  void initState() {
    super.initState();
    JlPlugin.instance.addEqChangeCallback(eqChangeCallBack);
    JlPlugin.instance.getEqSetting();
  }

  void eqChangeCallBack(EqInfo eqInfo){
    print("BlueEqSettingPage->eqInfo-:${eqInfo.toString()}");
    setState(() {
      eqConfig=eqInfo;
    });
  }

  @override
  void dispose() {
    super.dispose();
    JlPlugin.instance.removeEqChangeListener(eqChangeCallBack);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarLeftTitle(title: "EQ设置"),
      body: Column(
        children: [
          eqAdjustSetting(context),
          TextButton(onPressed: (){
            JlPlugin.instance.getEqSetting();
          }, child: Text("获取EQ设置"))
        ],
      ),
    );
  }

  EqInfo eqConfig=EqInfo();

  Widget eqAdjustSetting(BuildContext context) {
    var xAxis = [
      "32", "62", "125", "250", "500", "1K", "2K", "4K", "8K", "16K"
    ];
    var yAxis = [
      "10db", "5db", "0db", "-5db", "-10db"
    ];
    return Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16),
          color: Colors.white
      ),
      height: 260,
      width: double.infinity,
      margin: EdgeInsets.symmetric(horizontal: 16),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
      child: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Expanded(
            child: Row(
              mainAxisSize: MainAxisSize.max,
              children: [
                Padding(
                  padding: const EdgeInsets.only(top: 10, bottom: 25),
                  child: SizedBox(
                    width: 25,
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children:
                        yAxis.map((text) =>
                            Text(text, style: TextStyle(
                                color: c_989898, fontSize: 8),)).toList()
                    ),
                  ),
                ),
                SizedBox(width: 5),
                Expanded(
                  child: Container(
                    alignment: Alignment.topCenter,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: xAxis.map((_) {
                        var index = xAxis.indexOf(_);
                        var value = eqConfig.value[index];
                        return Column(
                          mainAxisSize: MainAxisSize.max,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Expanded(
                              child: SizedBox(
                                width: 18,
                                child: FlutterSlider(
                                    disabled: false,
                                    handlerHeight: 20,
                                    handlerWidth: 25,
                                    axis: Axis.vertical,
                                    // 设置为垂直方向
                                    min: -10.0,
                                    max: 10.0,

                                    rtl: true,
                                    values: [value.toDouble()],
                                    tooltip: FlutterSliderTooltip(
                                        textStyle: TextStyle(
                                            fontSize: 30, color: Colors.white),
                                        boxStyle: FlutterSliderTooltipBox(
                                            decoration: BoxDecoration(
                                                color: Colors.grey.withOpacity(
                                                    0.9)
                                            )
                                        )
                                    ),
                                    handler: FlutterSliderHandler(
                                      disabled: false,
                                      decoration: BoxDecoration(
                                          color: c_white, // 圆形滑块的颜色
                                          shape: BoxShape.circle,
                                          boxShadow: [BoxShadow(
                                            color: Colors.grey.withOpacity(0.5),
                                            blurRadius: 5,
                                            spreadRadius: 2,
                                          ),
                                          ]
                                      ),
                                      child: Container(
                                        width: 16, // 设置滑块的大小
                                        height: 16, // 设置滑块的大小
                                      ),
                                    ),
                                    jump: false,
                                    trackBar: const FlutterSliderTrackBar(
                                        activeTrackBarHeight: 10.0,
                                        inactiveTrackBarHeight: 10.0,
                                        inactiveTrackBar: BoxDecoration(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4)),
                                          color: c_f0f0f0,
                                        ),
                                        activeTrackBar: BoxDecoration(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4)),
                                          color: c_76f6ec,
                                        ),
                                        inactiveDisabledTrackBarColor: c_f0f0f0,
                                        activeDisabledTrackBarColor: c_76f6ec
                                    ),
                                    onDragCompleted: (handlerIndex, lowerValue,
                                        upperValue) {
                                    },
                                    onDragging: (handlerIndex, lowerValue,
                                        upperValue) {

                                      int settingValue=double.parse(lowerValue.toString()).toInt();
                                      setBandValue(index, settingValue);
                                    }),
                              ),
                            ),
                            SizedBox(height: 15,
                                child: Text(_, style: TextStyle(
                                    color: c_989898, fontSize: 8)))
                          ],
                        );
                      }).toList(),
                    ),),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
  Timer? _eqSettingTimer;
  void setBandValue(int index,int value){
    print("setBandValue: index:${index} value:${value} lll:${eqConfig.value.length}");
    eqConfig.value[index]=value;
    eqConfig.mode=EqInfo.MODE_CUSTOM;
    setState(() {

    });
    _eqSettingTimer?.cancel();
    // 启动新的定时器
    _eqSettingTimer = Timer(const Duration(milliseconds: 300), () {
      JlPlugin.instance.setEqSetting(eqConfig);
    });
  }

}